---
title: 'Measuring Subnational Trade Competitivess (by Huber et al.)'
author: Robert A. Huber, Yannick Stiller, Andreas Dür
output:
  rmdformats::downcute:
    self_contained: true
    thumbnails: true
    lightbox: true
    gallery: false
    highlight: tango
editor_options:
  chunk_output_type: console
---

```{r globopt, include = F}
knitr::opts_chunk$set(warning = FALSE)
```

In this notebook, we explain the replication of our data creation and analysis in more detail. 

# How to proceed

First download all files available from the Harvard Dataverse (see <https://doi.org/10.7910/DVN/BWRGUR>) in a folder of choice. The next steps of the analysis are outlined in the script below.

Note that we cannot share all necessary data to replicate the analyses for legal reasons. Information on where to obtain the data is available in the scripts or the Appendix (Table B1). 

# Prepare analysis

First download all files from the project file "Replication_STC.Rproj" to a folder of your choice. Open it in R-Studio and load the "README.Rmd" file. The next lines of code automatically set the working directory. 

```{r, message=FALSE}
# Set wd with here() package

here::i_am("Replication_STC.Rproj")

# Clean Environment
rm(list = ls())
```

The files should cover the following:

```{r}
#List of files
list.files()[-3] #-3 to exclude non_sharable data
list.files("./ancillary_data/")
list.files("./output/")
list.files("./rcode/")
```

Please ensure the folder structure is the same on your local device. 

# Calculation of STC

Open the R-Script "STC_calculation.R" from the subfolder rcode. It contains all steps to calculate our main dataset: stc.rds. We cannot share the underlying raw data. This is indicated by the file path "not_shareable" whenever data is loaded. Follow the instructions in the code to obtain the data. For the labour surveys, follow the links in the table below, which is based on the "keys.rds" file in ancillary_data.

```{r, echo=F, warning=F, message=FALSE}
library(tidyverse, verbose = F)

knitr::kable(readRDS("./ancillary_data/keys.rds") %>% 
               select(country_short, survey_source))
  
```

# Illustration of calculation with mock data

In order to illustrate the calculation, R-Script "Mock_calculation.R" in the subfolder rcode contains mock data that allows to replicate the calculation process for an illustrative mock case. All data is not related to a real-world case. 

# Replication of Evidence

Open the R-Script "Paper_replication.R" from the subfolder rcode. It contains all analyses presented in the paper and its appendix. Again, some data cannot be shared. This is indicated by the file path "not_shareable" whenever data is loaded. Follow the instructions in the code to obtain most data. 

# Session Info

This notebook was run using the following setup:

```{r}
pander::pander(sessionInfo())
```
